import Layout from '@/layout'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

/* 防止重复路由点击报错 */
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
	return originalReplace.call(this, location).catch(err => err)
}
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

const routes = [
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/login'),
		meta: { title: '登录页', hidden: true },
	},
	{
		path: '',
		component: Layout,
		redirect: 'home',
		meta: { title: '首页', hidden: true },
		children: [
			{
				path: 'home',
				name: 'Home',
				component: () => import('@/views/home'),
				meta: { title: '首页' },
			},
		],
	},
	{
		path: '/3DCase',
		component: Layout,
		redirect: '/3DCase/cesiumDemo',
		meta: { title: '3DCase', icon: 'el-icon-s-promotion' },
		//通过children配置子级路由
		children: [
			{
				path: 'cesiumDemo',
				name: 'cesiumDemo',
				component: () => import('@/views/3DCase/cesium'),
				meta: { title: '三维Cesium', icon: 'el-icon-menu' },
			},
		],
	},
	{
		path: '/echarts',
		component: Layout,
		redirect: '/echarts/echartsMap',
		meta: { title: 'echarts', icon: 'el-icon-pie-chart' },
		//通过children配置子级路由
		children: [
			{
				path: 'echartsMap',
				name: 'echartsMap',
				component: () => import('@/views/echarts/echartsMap'),
				meta: { title: 'echarts地图', icon: 'el-icon-menu' },
			},
			{
				path: 'echartsItem',
				name: 'echartsItem',
				component: () => import('@/views/echarts/echartsItem'),
				meta: { title: 'echarts图表', icon: 'el-icon-menu' },
			},
		],
	},
	{
		path: '/packageComponent',
		component: Layout,
		redirect: '/packageComponent/tablePage',
		meta: { title: '组件管理', icon: 'el-icon-collection' },
		//通过children配置子级路由
		children: [
			{
				path: 'richTextEditorPage',
				name: 'richTextEditorPage',
				component: () =>
					import('@/views/componentManagement/richTextEditorPage'),
				meta: { title: '富文本编辑器', icon: 'el-icon-document' },
			},
			{
				path: 'videoPlayer',
				name: 'videoPlayer',
				component: () => import('@/views/componentManagement/videoPlayer'),
				meta: { title: '视频播放器', icon: 'el-icon-video-camera-solid' },
			},
			{
				path: 'tablePage',
				name: 'tablePage',
				component: () => import('@/views/componentManagement/tablePage'),
				meta: { title: '表格', icon: 'el-icon-menu' },
			},
			{
				path: 'treePage',
				name: 'treePage',
				component: () => import('@/views/componentManagement/treePage'),
				meta: { title: '树形控件', icon: 'el-icon-menu' },
			},
			{
				path: 'selectTreePage',
				name: 'selectTreePage',
				component: () => import('@/views/componentManagement/selectTreePage'),
				meta: { title: '树形选择器', icon: 'el-icon-menu' },
			},
		],
	},
	{
		path: '/management',
		component: Layout,
		redirect: '/management/dictionaryManagement',
		meta: { title: '系统管理', icon: 'el-icon-setting' },
		//通过children配置子级路由
		children: [
			{
				path: 'dictionaryManagement',
				name: 'dictionaryManagement',
				component: () =>
					import(
						'@/views/management/dictionaryManagement/dictionaryManagement'
					),
				meta: { title: '字典管理', icon: 'el-icon-menu' },
			},
			{
				path: 'dictionaryData',
				name: 'dictionaryData',
				component: () =>
					import('@/views/management/dictionaryManagement/dictionaryData'),
				meta: { title: '字典数据', icon: 'el-icon-menu', hidden: true },
			},
			// {
			//   path: 'echartsItem',
			//   name: 'echartsItem',
			//   component: () => import('@/views/echarts/echartsItem'),
			//   meta: { title: 'echarts图表', icon: 'el-icon-menu' },
			// },
		],
	},
	{
		path: '/:catchAll(.*)',
		name: '404',
		meta: { title: '页面不存在', hidden: true },
		component: () => import('@/views/errorPage/error404'),
	},
]

const router = new VueRouter({
	mode: 'hash',
	routes,
	// 切换路由后滚动条置顶
	scrollBehavior() {
		return {
			x: 0,
			y: 0,
		}
	},
})

// 添加路由导航守卫
// 用来对访问的路由进行权限控制
// 除了login这个路由其他的路由都要进行了登录之后才能访问
router.beforeEach((to, from, next) => {
	if (to.path === '/login' || window.sessionStorage.getItem('user'))
		return next()
	return next('/login')
})

export default router
